package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.WmsMaterialCategoryMapper;
import com.ruoyi.domain.WmsMaterialCategory;
import com.ruoyi.service.IWmsMaterialCategoryService;

import javax.annotation.Resource;

/**
 * 物料分类Service业务层处理
 * 
 * @author yinqing
 * @date 2025-06-13
 */
@Service
public class WmsMaterialCategoryServiceImpl implements IWmsMaterialCategoryService 
{
    @Resource
    private WmsMaterialCategoryMapper wmsMaterialCategoryMapper;

    /**
     * 查询物料分类
     * 
     * @param id 物料分类主键
     * @return 物料分类
     */
    @Override
    public WmsMaterialCategory selectWmsMaterialCategoryById(Long id)
    {
        return wmsMaterialCategoryMapper.selectWmsMaterialCategoryById(id);
    }

    /**
     * 查询物料分类列表
     * 
     * @param wmsMaterialCategory 物料分类
     * @return 物料分类
     */
    @Override
    public List<WmsMaterialCategory> selectWmsMaterialCategoryList(WmsMaterialCategory wmsMaterialCategory)
    {
        return wmsMaterialCategoryMapper.selectWmsMaterialCategoryList(wmsMaterialCategory);
    }

    @Override
    public List<TreeSelect> selectCategoryTreeList(WmsMaterialCategory category) {
        List<WmsMaterialCategory> depts = SpringUtils.getAopProxy(this).selectWmsMaterialCategoryList(category);
        return buildCategoryTreeSelect(depts);
    }

    @Override
    public List<WmsMaterialCategory> buildCategoryTree(List<WmsMaterialCategory> categorys) {
        List<WmsMaterialCategory> returnList = new ArrayList<WmsMaterialCategory>();
        List<Long> tempList = categorys.stream().map(WmsMaterialCategory::getId).collect(Collectors.toList());
        for (WmsMaterialCategory category : categorys)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categorys, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categorys;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildCategoryTreeSelect(List<WmsMaterialCategory> categorys) {
        List<WmsMaterialCategory> deptTrees = buildCategoryTree(categorys);
        return deptTrees.stream().map(dept-> {
            TreeSelect treeSelect = new TreeSelect();
            treeSelect.setId(dept.getId());
            treeSelect.setLabel(dept.getName());
            List<WmsMaterialCategory> children = (List<WmsMaterialCategory>) dept.getChildren();
            if(ObjectUtils.isNotEmpty(children)){
                treeSelect.setChildren(buildCategoryTreeSelect(children));
            }
            return treeSelect;
        }).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<WmsMaterialCategory> list, WmsMaterialCategory t)
    {
        // 得到子节点列表
        List<WmsMaterialCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (WmsMaterialCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<WmsMaterialCategory> getChildList(List<WmsMaterialCategory> list, WmsMaterialCategory t)
    {
        List<WmsMaterialCategory> tlist = new ArrayList<WmsMaterialCategory>();
        Iterator<WmsMaterialCategory> it = list.iterator();
        while (it.hasNext())
        {
            WmsMaterialCategory n = (WmsMaterialCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<WmsMaterialCategory> list, WmsMaterialCategory t)
    {
        return getChildList(list, t).size() > 0;
    }
    /**
     * 新增物料分类
     * 
     * @param wmsMaterialCategory 物料分类
     * @return 结果
     */
    @Override
    public int insertWmsMaterialCategory(WmsMaterialCategory wmsMaterialCategory)
    {
        return wmsMaterialCategoryMapper.insertWmsMaterialCategory(wmsMaterialCategory);
    }

    /**
     * 修改物料分类
     * 
     * @param wmsMaterialCategory 物料分类
     * @return 结果
     */
    @Override
    public int updateWmsMaterialCategory(WmsMaterialCategory wmsMaterialCategory)
    {
        return wmsMaterialCategoryMapper.updateWmsMaterialCategory(wmsMaterialCategory);
    }

    /**
     * 批量删除物料分类
     * 
     * @param ids 需要删除的物料分类主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialCategoryByIds(Long[] ids)
    {
        return wmsMaterialCategoryMapper.deleteWmsMaterialCategoryByIds(ids);
    }

    /**
     * 删除物料分类信息
     * 
     * @param id 物料分类主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialCategoryById(Long id)
    {
        return wmsMaterialCategoryMapper.deleteWmsMaterialCategoryById(id);
    }
}
